package com.deer.wms.kuaidiniao.web;

import com.deer.wms.system.manage.model.organization.Organization;
import com.deer.wms.system.manage.model.ware.WareInfo;
import com.deer.wms.system.manage.service.OrganizationService;
import com.deer.wms.system.manage.service.WareInfoService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.kuaidiniao.model.PrintParam;
import com.deer.wms.kuaidiniao.model.PrintParamCriteria;
import com.deer.wms.kuaidiniao.service.PrintParamService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.system.manage.model.user.UserInfo;
import com.deer.wms.system.manage.service.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
* Created by ll on 2020/02/12.
*/
@RestController
@RequestMapping("/print/params")
public class PrintParamController {

    @Autowired
    private PrintParamService printParamService;
    @Autowired
    private WareInfoService wareInfoService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private UserInfoService userInfoService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/add")
    public Result add(@RequestBody PrintParam printParam,  @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        UserInfo user = userInfoService.getById(printParam.getUserId());
        WareInfo ware = wareInfoService.getById(printParam.getWareId());
        Organization organization = organizationService.getById(printParam.getOrganizationId());
        printParam.setUserName(user.getUserName());
        printParam.setWareName(ware.getWareName());
        printParam.setOrganizationName(organization.getOrganizationName());
        printParamService.save(printParam);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        printParamService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/update")
    public Result update(@RequestBody PrintParam printParam, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        printParamService.updateById(printParam);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        PrintParam printParam = printParamService.getById(id);
        return ResultGenerator.genSuccessResult(printParam);
    }

    @GetMapping("/list")
    public Result list(PrintParamCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<PrintParam> list = printParamService.list();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "新增SO信息", notes = "新增SO信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/myConfigs")
    public Result findMyConfigs(@RequestBody PrintParamCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<PrintParam> list = printParamService.findMyConfig(currentUser.getUserId());
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}